package andev.axplorer.FileWatcher;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.widget.LinearLayout;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import andev.aandroidlib.activity.AActivity;
import andev.aandroidlib.util.FileOperator;
import andev.aandroidlib.view.MatrixImageView;
import andev.axplorer.R;

/**
 * Created by lzy1g on 2015/8/10.
 */
public class ImageFileWatcher extends AActivity {
    private LinearLayout ivContainer;
    private MatrixImageView MIVLeft, MIVMiddle, MIVRight;
    private Bitmap image;
    private ArrayList<String> sameFolderImagePaths = new ArrayList<>();
    private String nowFilePath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_filewatcher);

        overridePendingTransition(R.anim.abc_popup_enter, R.anim.abc_fade_out);

        ivContainer = (LinearLayout) findViewById(R.id.ivContainer);
        MIVMiddle = new MatrixImageView(this);
        ivContainer.addView(MIVMiddle);
        MIVMiddle.setDragListener(new MatrixImageView.DragListener() {
            @Override
            public void dragLeft() {
                int nowIndex = sameFolderImagePaths.lastIndexOf(nowFilePath);
                if (nowIndex < sameFolderImagePaths.size() - 1) {
                    nowFilePath = sameFolderImagePaths.get(++nowIndex);
                    image = BitmapFactory.decodeFile(nowFilePath);
                    MIVRight = new MatrixImageView(ImageFileWatcher.this);
                    ivContainer.addView(MIVRight);
                }
            }

            @Override
            public void dragRight() {
                int nowIndex = sameFolderImagePaths.lastIndexOf(nowFilePath);
                if (nowIndex > 0) {
                    nowFilePath = sameFolderImagePaths.get(--nowIndex);
                    image = BitmapFactory.decodeFile(nowFilePath);
                    MIVLeft = new MatrixImageView(ImageFileWatcher.this);
                    ivContainer.addView(MIVLeft, 0);
                }
            }
        });

        InitData();

        MIVMiddle.setSwipeListener(new MatrixImageView.SwipeListener() {
            @Override
            public void swipeLeft() {
                int nowIndex = sameFolderImagePaths.lastIndexOf(nowFilePath);
                if (nowIndex < sameFolderImagePaths.size() - 1) {
                    nowFilePath = sameFolderImagePaths.get(++nowIndex);
                    image = BitmapFactory.decodeFile(nowFilePath);
                    MIVMiddle.changeImageBitmap(image, true);
                }
            }

            @Override
            public void swipeRight() {
                int nowIndex = sameFolderImagePaths.lastIndexOf(nowFilePath);
                if (nowIndex > 0) {
                    nowFilePath = sameFolderImagePaths.get(--nowIndex);
                    image = BitmapFactory.decodeFile(nowFilePath);
                    MIVMiddle.changeImageBitmap(image, false);
                }
            }
        });
    }

    private void InitData() {
        File file = null;
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            file = (File) bundle.getSerializable("file");
        } else {
            Uri uri = intent.getData();
            try {
                file = new File(new URI(uri.toString()));
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
        nowFilePath = file.getAbsolutePath();
        image = BitmapFactory.decodeFile(nowFilePath);
        MIVMiddle.setImageBitmap(image);

        final FileOperator fileOperator = new FileOperator();
        fileOperator.crawlDir(this, file.getParentFile(), new FileOperator.CrawlListener() {
            @Override
            public boolean onCrawlHandleData(String absolutePath) {
                switch (fileOperator.getFileExtension(new File(absolutePath))) {
                    case ".jpg":
                    case ".jpeg":
                    case ".gif":
                    case ".bmp":
                    case ".png":
                        sameFolderImagePaths.add(absolutePath);
                        return false;
                    default:
                        return false;
                }
            }

            @Override
            public void onCrawlHandleUI(String absolutePath) {

            }

            @Override
            public void onCrawlEnd() {

            }
        });
    }
}
